package cn.imnu.lowcodeformgeneratorbackend.service.impl;

import cn.imnu.lowcodeformgeneratorbackend.dto.auth.LoginRequest;
import cn.imnu.lowcodeformgeneratorbackend.dto.auth.RegisterRequest;
import cn.imnu.lowcodeformgeneratorbackend.dto.auth.UserResponse;
import cn.imnu.lowcodeformgeneratorbackend.entity.User;
import cn.imnu.lowcodeformgeneratorbackend.mapper.UserMapper;
import cn.imnu.lowcodeformgeneratorbackend.service.UserService;
import cn.imnu.lowcodeformgeneratorbackend.util.JwtUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private JwtUtil jwtUtil;

    // 登录
    @Override
    public String login(LoginRequest loginRequest) {
        User user = userMapper.findByUsername(loginRequest.getUsername());
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        if (user.getStatus() == 0) {
            throw new RuntimeException("用户已被禁用");
        }

        if (!passwordEncoder.matches(loginRequest.getPassword(), user.getPassword())) {
            throw new RuntimeException("密码错误");
        }

        // 更新最后登录时间
        updateLastLoginTime(user.getId());

        return jwtUtil.generateToken(user.getUsername(), user.getId());
    }
    // 注册
    @Override
    public UserResponse register(RegisterRequest registerRequest) {
        // 检查用户名是否已存在
        if (userMapper.findByUsername(registerRequest.getUsername()) != null) {
            throw new RuntimeException("用户名已存在");
        }

        // 检查邮箱是否已存在
        if (registerRequest.getEmail() != null && userMapper.findByEmail(registerRequest.getEmail()) != null) {
            throw new RuntimeException("邮箱已被注册");
        }

        User user = new User();
        user.setUsername(registerRequest.getUsername());
        user.setPassword(passwordEncoder.encode(registerRequest.getPassword()));
        user.setEmail(registerRequest.getEmail());
        user.setNickname(registerRequest.getNickname());
        user.setStatus(1);
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());

        userMapper.insert(user);

        UserResponse response = new UserResponse();
        BeanUtils.copyProperties(user, response);
        return response;
    }
    // 获取用户信息
    @Override
    public UserResponse getUserInfo(Long userId) {
        User user = userMapper.selectById(userId);
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }

        UserResponse response = new UserResponse();
        BeanUtils.copyProperties(user, response);
        return response;
    }
    // 用户名查询
    @Override
    public User findByUsername(String username) {
        return userMapper.findByUsername(username);
    }
    // 更新最后登录时间
    @Override
    public boolean updateLastLoginTime(Long userId) {
        User user = new User();
        user.setId(userId);
        user.setLastLoginTime(LocalDateTime.now());
        return userMapper.updateById(user) > 0;
    }
}
